package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.NoteList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/5/6.
 */

public class MySendAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<NoteList.ListBean> list;
    private LayoutInflater layoutInflater;
    private boolean bottom;
    private int bottom_count, tag;//是否显示底部，底部显示文字

    public MySendAdapter(Context context, List<NoteList.ListBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    public void UpData(List<NoteList.ListBean> list, int bottom_count, int tag) {
        this.list = list;
        this.bottom_count = bottom_count;
        this.tag = tag;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = layoutInflater.inflate(R.layout.manager_item, viewGroup, false);
        View view_bottom = layoutInflater.inflate(R.layout.bottom_view, viewGroup, false);
        AutoUtils.autoSize(view);
        AutoUtils.autoSize(view_bottom);
        if(i==ManagerItem.Bottom){
            return new BottomHolder(view_bottom);
        }else{
            return new ManagerViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ManagerViewHolder){
            ((ManagerViewHolder) holder).position=position;
            ((ManagerViewHolder) holder).manager_name.setVisibility(View.GONE);
            if(list!=null&&list.size()!=0){
                ((ManagerViewHolder) holder).manager_title.setText(list.get(position).getTitle());
                ((ManagerViewHolder) holder).manager_time.setText(list.get(position).getCreateTime());

                if(list.get(position).getStatus()==2){
                    ((ManagerViewHolder) holder).manager_num.setText("已撤销");
                }else{
                    ((ManagerViewHolder) holder).manager_num.setText("已成交"+list.get(position).getDealCount()+"次");
                }
                switch (list.get(position).getPriceMethod()){
                    case 0:
                        ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/次");//服务单价
                        break;
                    case 1:
                        ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/时");//服务单价
                        break;
                    case 2:
                        ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/天");//服务单价
                        break;
                    case 3:
                        ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/周");//服务单价
                        break;
                    case 4:
                        ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/月");//服务单价
                        break;
                    case 5:
                        ((ManagerViewHolder) holder).manager_price.setText(list.get(position).getPrice()+"/年");//服务单价
                        break;
                }
            }
        }else if(holder instanceof BottomHolder){
            if(bottom_count==1&&tag==0){
                ((BottomHolder) holder).bottom_tv.setText("松开加载");
            }else if(bottom_count==1&&tag==1){
                ((BottomHolder) holder).bottom_tv.setText("正在加载");
            }else if(bottom_count==1&&tag==2){
                ((BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(list!=null&&position==list.size()){
            return ManagerItem.Bottom;
        }else{
            return super.getItemViewType(position);
        }

    }

    @Override
    public int getItemCount() {
        return list==null?0+bottom_count:list.size()+bottom_count;
    }

    class ManagerViewHolder extends RecyclerView.ViewHolder {
        int position;
        RelativeLayout manager_item_view;
        TextView manager_title, manager_classify, manager_time, manager_price, manager_num, manager_name;
        public ManagerViewHolder(View itemView) {
            super(itemView);
            manager_item_view = (RelativeLayout) itemView.findViewById(R.id.manager_item_view);
            manager_title = (TextView) itemView.findViewById(R.id.manager_title);
            manager_classify = (TextView) itemView.findViewById(R.id.manager_classify);
            manager_time = (TextView) itemView.findViewById(R.id.manager_time);
            manager_price = (TextView) itemView.findViewById(R.id.manager_price);
            manager_num = (TextView) itemView.findViewById(R.id.manager_num);
            manager_name = (TextView) itemView.findViewById(R.id.manager_name);
            manager_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Gson gson = new Gson();
                    String json = gson.toJson(list.get(position));
                    MyMethod.toActivity(context, NoteDetail.class, new String[]{"json", "tag"}, new String[]{json, ManagerItem.WeiZhi + ""});
                }
            });
            manager_item_view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    new AlertDialog.Builder(context).setMessage("是否删除").setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            DeleteNote(position);
                            dialogInterface.dismiss();
                        }
                    }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).show();
                    return false;
                }
            });
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        TextView bottom_tv;

        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv = (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }

    private void DeleteNote(final int position){
        MyOkHttpUtils.getInstance(context).doDetele(Constants.MyOrder+"/"+list.get(position).getId())
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        if(message!=null){
                            try {
                                JSONObject jsonObject = new JSONObject(message);
                                boolean result = jsonObject.getBoolean("result");
                                if(result){
                                    MyMethod.ShowDialog(context,"撤销成功");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
